package com.chainyoung.common.utils;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.widget.Toast;

import es.dmoral.toasty.Toasty;


/**
 * 作者：alsoWell on 2017/5/3 15:53
 * 邮箱：1161882463@qq.com
 */
public class ToastUtil {
    private static Context mContext;
    private static Toast mNormalToast;
    public static void init(Context context) {
        mContext = context;
        Toasty.Config.getInstance()
                .setErrorColor(Color.BLACK) // optional
                .setInfoColor(Color.BLACK) // optional
                .setSuccessColor(Color.BLACK) // optional
                .setWarningColor(Color.BLACK) // optional
                .setTextColor(Color.WHITE) // optional
                .tintIcon(false) // optional (apply textColor also to the icon)
                .setToastTypeface(Typeface.DEFAULT) // optional
                .apply(); // required

    }
    public ToastUtil() {

    }
    /*****************************************显示正确的提示*****************************************/

    private static Toast mToast;
    /**
     * 显示单个吐司 不重复显示
     *
     * @param msg
     */
    public static void showToast(String msg) {

        if (mNormalToast == null) {
            mNormalToast = Toast.makeText(mContext, msg,
                    Toast.LENGTH_SHORT);
        }
        mNormalToast.setText(msg);
        mNormalToast.show();
    }

    /**
     * 显示单个吐司 根据文件资源
     * @param resID
     */
    public static void showToast(int resID) {
        String mS = mContext.getResources().getString(resID);
        if (mToast == null) {
            mToast = Toast.makeText(mContext,mS ,
                    Toast.LENGTH_SHORT);
        }
        mToast.setText(mS);
        mToast.show();
    }


    public static void showCorrectToast(String msg, boolean withIcon) {
        mToast=Toasty.success(mContext, msg, Toast.LENGTH_SHORT, withIcon);
        mToast.show();
    }
    public static void showCorrectToast(String msg) {
        showCorrectToast(msg,false);
    }

    public static void showCorrectToast(int rsID,boolean withIcon) {
        mToast=Toasty.success(mContext, mContext.getResources().getString(rsID), Toast.LENGTH_SHORT, withIcon);
        mToast.show();
    }
    public static void showCorrectToast(int rsID) {
        showCorrectToast(rsID,false);
    }
    /*****************************************显示错误的提示*****************************************/
    public static void showErrorToast(String msg, boolean withIcon) {
        mToast=Toasty.error(mContext, msg, Toast.LENGTH_SHORT, withIcon);
        mToast.show();
    }
    public static void showErrorToast(String msg) {
        showErrorToast(msg,false);
    }

    public static void showErrorToast(int rsID,boolean withIcon) {
        mToast=Toasty.error(mContext, mContext.getResources().getString(rsID), Toast.LENGTH_SHORT, withIcon);
        mToast.show();
    }
    public static void showErrorToast(int rsID) {
        showErrorToast(rsID,false);
    }
    /*****************************************显示警告的提示*****************************************/
    public static void showWarningToast(String msg, boolean withIcon) {
        mToast = Toasty.warning(mContext, msg, Toast.LENGTH_SHORT, withIcon);
        mToast.show();
    }
    public static void showWarningToast(String msg) {
        showWarningToast(msg,false);
    }

    public static void showWarningToast(int rsID,boolean withIcon) {
        mToast= Toasty.warning(mContext, mContext.getResources().getString(rsID), Toast.LENGTH_SHORT, withIcon);
        mToast.show();
    }
    public static void showWarningToast(int rsID) {
        showWarningToast(rsID,false);
    }
    /*****************************************显示info的提示*****************************************/
    public static void showInfoToast(String msg, boolean withIcon) {
       mToast= Toasty.info(mContext, msg, Toast.LENGTH_SHORT, withIcon);
       mToast.show();
    }
    public static void showInfoToast(String msg) {
        showInfoToast(msg,false);
    }

    public static void showInfoToast(int rsID,boolean withIcon) {
        mToast=Toasty.info(mContext, mContext.getResources().getString(rsID), Toast.LENGTH_SHORT, withIcon);
        mToast.show();
    }
    public static void showInfoToast(int rsID) {
        showInfoToast(rsID,false);
    }

    /*****************************************显示普通的提示*****************************************/
    public static void showNormalToast(String msg) {
        mToast = Toasty.custom(mContext, msg, null, Toast.LENGTH_SHORT, false);
        mToast.show();
    }
    public static void showNormalToast(int rsID) {
        mToast = Toasty.custom(mContext, mContext.getResources().getString(rsID), null, Toast.LENGTH_SHORT, false);
        mToast.show();
    }

}
